package cool.taomu.toolkit.retry.test

import com.google.inject.AbstractModule
import cool.taomu.toolkit.Taomu
import cool.taomu.toolkit.platform.inter.CreateChildInjector
import cool.taomu.toolkit.retry.Retry
import java.util.concurrent.CompletableFuture

class QuartzTest {

    @Retry(maxAttempts=3, delay=500, exponentialBackoff=true, rateLimit=2.0)
    def CompletableFuture<Object> callExternalService() {
        System.out.println("调用外部服务...");
        if (Math.random() < 0.7) {
            throw new RuntimeException("失败");
        }
        return CompletableFuture.completedFuture("成功");
    }

    def static void main(String[] arg) {
        var inj = Taomu.run().createChildInjector(new AbstractModule() {
            override configure() {
                bind(QuartzTest)
            }
        })
        var qt = inj.getInstance(QuartzTest)
        var a = qt.callExternalService().get()
        /*.thenAccept([ res |
            System.out.println("最终结果: " + res);
        ]).exceptionally([ ex |
            System.err.println("错误: " + ex.getMessage());
            return null;
        ]);*/
        System.out.println(a)

        try {

            Thread.sleep(10000);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
